package com.hongxun.budget.ui.view.ys;

import android.app.Fragment;
import android.widget.Spinner;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.adapter.SpinnerAdapter;
import com.hongxun.budget.ui.view.BaseView;
import com.hongxun.budget.utils.GreenDaoManager;
import com.hongxun.budget.utils.SPUtils;
import com.hongxun.greenentity.tk.DPointEntity;
import com.hongxun.greenentity.ys.EPointEntity;

import java.util.Arrays;

/**
 * Created by admin on 2018/8/27.
 */

public class YsDahuodianView extends BaseView {

    Spinner spinner;
    SpinnerAdapter<String> adapter;

    public YsDahuodianView(Fragment fragment) {
        super(fragment);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.view_ys_dahuodian;
    }

    @Override
    protected void initView() {
        super.initView();
        spinner = getView(R.id.spinner);
        spinner.setAdapter(adapter = new SpinnerAdapter<String>(context, Arrays.asList(context.getResources().getStringArray(R.array.dahuodian_mode)), spinner));
    }

    EPointEntity d;

    @Override
    public boolean save(EPointEntity d) {
        d.setPPPropertythree(adapter.getSelectedItem());//接线方式
        this.d = d;
        return true;
    }

    @Override
    public void insertDB() {
        GreenDaoManager.getInstance().getmDaoSession().getEPointEntityDao().insertOrReplace(d);
    }

    @Override
    public void updateInit(Object o) {
        EPointEntity d = (EPointEntity) o;
        String[] array = context.getResources().getStringArray(R.array.dahuodian_mode);
        for (int i = 0; i < array.length; i++) {
            if (d.getPPPropertythree().equals(array[i])) {
                spinner.setSelection(i);
                break;
            }
        }
    }

    @Override
    public void initDefault() {
        EPointEntity d = GreenDaoManager.getInstance().getmDaoSession().getEPointEntityDao().load(SPUtils.get(context, "UUID", "").toString());
        if (d != null) updateInit(d);
    }
}
